package com.towncoder.binding;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * 对接口的代理类的统一封装
 */
public class MapperProxy<T> implements InvocationHandler, Serializable {

    private final Class<T> mapperInterface;
    private Map<String, String> sqlSession;  // 每个mapper接口的连接信息

    public MapperProxy(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    @Override
    /**
     * 每个接口方法的实现的统一处理
     */
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // method.getDeclaringClass() 一个方法所属的类
        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this, args);
        } else {
            return "你的被代理了！";
//            + sqlSession.get(mapperInterface.getName() + "." + method.getName())
        }
    }
}
